public class MyQueue {
    static class Node{
        public Node next;
        public Node prev;
        public int val;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node rear;//队尾
    public Node front;//队头

    // ⼊队列---向双向链表位置插⼊新节点
    public int offer(int e){
        Node newN=new Node(e);
        if(front==null){
            rear=newN;
            front=newN;
        }else{
            newN.prev=rear;
            rear.next=newN;
            rear=newN;

        }
        return e;
    }

    // 出队列---将双向链表第⼀个节点删除掉
    public int poll(){
        if(front==null){
            throw new RuntimeException("队列为空，删除失败！");
        }
        int ret = front.val;
        front=front.next;
        //避免只有一个节点
        if(front!=null){
            front.prev=null;
        }
        else{
            rear=null;
        }
        return ret;
    }
    // 获取队头元素---获取链表中第⼀个节点的值域
    public int peek(){
        if(front==null){
            throw new RuntimeException("队列没有元素，获取失败！");
        }
        return front.val;
    }
    public int size() {
        int count=0;
        Node cur=front;
        while(cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }
    public boolean isEmpty(){
        return size()==0;
    }
}
